import { ibootSimpleResponseObject, Page, PageModel } from "@/utils/request";
import { LayoutType, MenuDataItem, TabDataItem, Theme } from "@iboot-vue/antd-component2/es/layouts/lib";
import { LocaleType } from "@iboot-vue/antd-component2/es/locale";
import { DefaultResponse } from "@iboot-vue/common";
import { AliasToken } from 'ant-design-vue/es/theme/internal';
import { message } from 'ant-design-vue';

export type LayoutConfigType = {
  prefixCls: string;
  logo: string;
  systemName: string;
  version: string
  theme: Theme;
  layout?: LayoutType;
  locale?: LocaleType;
  collapsed?: boolean;
  menuData: MenuDataItem[];
  selectedKeys: string[];
  openKeys: string[];
  tabActiveKey: string;
  tabs: TabDataItem[];
  themeToken?: AliasToken;
};

export type Post = {
  id?: number;
  userId: string;
  title: string;
  body: string;
}


export function getLayoutConfig(): Promise<LayoutConfigType> {
  return commonRequest($api.GET('/layoutConfig'));
}

export function pagePost(param: Page): Promise<PageModel<Post>> {
  return commonRequest($api.POST('/posts/page', param));
}

export function createPost(param: Post): Promise<Post> {
  return commonRequest($api.PUT('/posts/create', param));
}

export function deletePost(id: number): Promise<string> {
  return ibootSimpleResponseObject($api.DELETE(`/posts/${id}`));
}

export function deleteBatchPost(ids: number[]): Promise<string> {
  return ibootSimpleResponseObject($api.DELETE(`/posts/batchDelete`, ids));
}

export const commonRequest = <T>(request: Promise<DefaultResponse>
): Promise<T> => {
  return  ibootSimpleResponseObject(request, result => {
    if (result) {
      if (typeof result === 'string') {
        message.error(result);
      } else {
        if (result.error) {
          if (result.error.code === '-1') {
            message.error(result.error.msg);
          } else {
            message.warning(result.error.msg);
          }
        } else {
          message.error(result.msg);
        }
      }
    } else {
      message.error('错误');
    }
  });
};
